package car2017_demo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.serialport.SerialPort;
import android.util.Log;
import android.widget.Toast;

import com.bkrc.camera.XcApplication;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.DecodeHintType;
import com.google.zxing.FormatException;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import car.bkrc.com.car2018.FirstActivity;
import car.bkrc.right.fragment.LeftFragment;
import shape.shape;
import util.FileUtil;

public class connect_transport {
    public int Stop_Flag;
    public int DIR = 1;
    public int POSITION = 0;
    public int RFID_POSITION;

    //处理结果
    public String result_qr;
    public String result_plate;
    public String result_shape;
    public int result_traffic;
    public String result_openmv;
    //车的状态
    public long ultraSonic;
    public long light;
    public long codedDisk;
    public long psStatus;
    public byte[] result_rfid = new byte[16];

    public static DataInputStream bInputStream = null;
    public static DataOutputStream bOutputStream = null;
    public static Socket socket = null;
    public byte[] rbyte = new byte[50];
    private Handler reHandler;
    public short TYPE = 0xAA;
    public short MAJOR = 0x00;
    public short FIRST = 0x00;
    public short SECOND = 0x00;
    public short THRID = 0x00;
    public short CHECKSUM = 0x00;

    public String TAG = "connect_transport";



    private static OutputStream SerialOutputStream;
    private InputStream SerialInputStream;
    private boolean Firstdestroy = false;  ////Firstactivity 是否已销毁了
    public Context context;
    private Date date = new Date();
    public Timer timer;

    public connect_transport(Context context1) {
        context = context1;
    }

    public void destory() {
        try {
            if (socket != null && !socket.isClosed()) {
                socket.close();
                bInputStream.close();
                bOutputStream.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void connect(Handler reHandler, String IP) {
        try {
            this.reHandler = reHandler;
            Firstdestroy = false;
            int port = 60000;
            socket = new Socket(IP, port);
            bInputStream = new DataInputStream(socket.getInputStream());
            bOutputStream = new DataOutputStream(socket.getOutputStream());
            reThread.start();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void serial_connect(Handler reHandler) {
        this.reHandler = reHandler;
        try {
            int baudrate = 115200;
            String path = "/dev/ttyS4";
            SerialPort mSerialPort = new SerialPort(new File(path), baudrate, 0);
            SerialOutputStream = mSerialPort.getOutputStream();
            SerialInputStream = mSerialPort.getInputStream();
            //new Thread(new SerialRunnable()).start();
            //reThread.start();
        } catch (IOException e) {
            e.printStackTrace();
        }

        XcApplication.executorServicetor.execute(new SerialRunnable());
        //new Thread(new serialRunnable()).start();
    }

    byte[] serialreadbyte = new byte[50];

    class SerialRunnable implements Runnable {
        @Override
        public void run() {
            while (SerialInputStream != null) {
                try {
                    int num = SerialInputStream.read(serialreadbyte);
                    // String  readserialstr =new String(serialreadbyte);
                    String readserialstr = new String(serialreadbyte, 0, num, "utf-8");
                    Log.e("----serialreadbyte----", "******" + readserialstr);
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = serialreadbyte;
                    reHandler.sendMessage(msg);
                    /*
                    for (int i = 0; i < num; i++) {
                        Log.e("----serialreadbyte----", "******" +Integer.toHexString(serialreadbyte[i]));
                      //  Log.e("----serialreadbyte----", "******" + serialreadbyte[i]);
                    }
                    */
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private Thread reThread = new Thread(new Runnable() {
        @Override
        public void run() {
            // TODO Auto1-generated method stub
            while (socket != null && !socket.isClosed()) {
                if (Firstdestroy)  //Firstactivity 已销毁了
                {
                    break;
                }
                try {
                    bInputStream.read(rbyte);
                    // 光敏状态
                    psStatus = rbyte[3] & 0xff;
                    // 超声波数据
                    ultraSonic = rbyte[5] & 0xff;
                    ultraSonic = ultraSonic << 8;
                    ultraSonic += rbyte[4] & 0xff;
                    // 光照强度
                    light = rbyte[7] & 0xff;
                    light = light << 8;
                    light += rbyte[6] & 0xff;
                    // 码盘
                    codedDisk = rbyte[9] & 0xff;
                    codedDisk = codedDisk << 8;
                    codedDisk += rbyte[8] & 0xff;
                    DIR = rbyte[10] & 0xff;//车的方向
                    POSITION = rbyte[11] & 0xff;//车的位置
                    if(rbyte[1] != (byte) 0x02) {
                        RFID_POSITION = rbyte[12] & 0xff;//RFID卡的位置
                        for (int i = 0; i < 16; i++) {
                            //RFID卡的内容
                            result_rfid[i] = rbyte[i + 13];
                        }
                    }
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = rbyte;
                    reHandler.sendMessage(msg);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    });

    @SuppressLint("HandlerLeak")
    public Handler myHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 10:
                    Toast.makeText(context, "识别结果为：" + result_qr, Toast.LENGTH_LONG).show();
                    break;
                case 11:
                    Toast.makeText(context, "图片为null", Toast.LENGTH_LONG).show();
                    break;
                case 30:
                    Toast.makeText(context, "识别结果为：" + result_plate, Toast.LENGTH_LONG).show();
                    break;
                case 31:
                    Toast.makeText(context, "图片为不存在", Toast.LENGTH_LONG).show();
                    break;
                default:
                    break;
            }
        }
    };

    private void send() {
        CHECKSUM = (short) ((MAJOR + FIRST + SECOND + THRID) % 256);
        // 发送数据字节数组

        final byte[] sbyte = {0x55, (byte) TYPE, (byte) MAJOR, (byte) FIRST, (byte) SECOND, (byte) THRID, (byte) CHECKSUM, (byte) 0xBB};

        if (XcApplication.isserial == XcApplication.Mode.SOCKET) {
            XcApplication.executorServicetor.execute(new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    try {
                        if (socket != null && !socket.isClosed()) {
                            bOutputStream.write(sbyte, 0, sbyte.length);
                            bOutputStream.flush();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        } else if (XcApplication.isserial == XcApplication.Mode.SERIAL) {

            XcApplication.executorServicetor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        SerialOutputStream.write(sbyte, 0, sbyte.length);
                        SerialOutputStream.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        } else if (XcApplication.isserial == XcApplication.Mode.USB_SERIAL)
            try {
                FirstActivity.sPort.write(sbyte, 5000);
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    public void send_voice(final byte[] textbyte) {
        if (XcApplication.isserial == XcApplication.Mode.SOCKET) {
            XcApplication.executorServicetor.execute(new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    try {
                        if (socket != null && !socket.isClosed()) {
                            bOutputStream.write(textbyte, 0, textbyte.length);
                            bOutputStream.flush();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        } else if (XcApplication.isserial == XcApplication.Mode.SERIAL) {

            XcApplication.executorServicetor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        SerialOutputStream.write(textbyte, 0, textbyte.length);
                        SerialOutputStream.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        } else if (XcApplication.isserial == XcApplication.Mode.USB_SERIAL)
            try {
                FirstActivity.sPort.write(textbyte, 5000);
            } catch (IOException e) {
                e.printStackTrace();
            }

				/*
				try {
					// ���������ֽ�����
					if (socket != null && !socket.isClosed()) {
						bOutputStream.write(textbyte, 0, textbyte.length);
						bOutputStream.flush();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
				*/
    }


    // 前进
    public void go(int sp_n, int en_n) {
        MAJOR = 0x02;
        FIRST = (byte) (sp_n & 0xFF);
        SECOND = (byte) (en_n & 0xff);
        THRID = (byte) (en_n >> 8);
        send();
    }

    // 后退
    public void back(int sp_n, int en_n) {
        MAJOR = 0x03;
        FIRST = (byte) (sp_n & 0xFF);
        SECOND = (byte) (en_n & 0xff);
        THRID = (byte) (en_n >> 8);
        send();
    }


    //左转
    public void Left(int sp_n) {
        MAJOR = 0x04;
        FIRST = (byte) (sp_n & 0xFF);
        SECOND = (byte) 0x00;
        THRID = (byte) 0x00;
        send();
    }

    //码盘转弯
    public void Left_mp(int sp_n, int en_n) {
        MAJOR = 0x08;
        FIRST = (byte) (sp_n & 0xFF);
        SECOND = (byte) (en_n & 0xff);
        THRID = (byte) (en_n >> 8);
        send();
    }


    // 右转
    public void Right(int sp_n) {
        MAJOR = 0x05;
        FIRST = (byte) (sp_n & 0xFF);
        SECOND = (byte) 0x00;
        THRID = (byte) 0x00;
        send();
    }

    //码盘转弯
    public void Right_mp(int sp_n, int en_n) {
        MAJOR = 0x09;
        FIRST = (byte) (sp_n & 0xFF);
        SECOND = (byte) (en_n & 0xff);
        THRID = (byte) (en_n >> 8);
        send();
    }

    // 停车
    public void stop() {
        MAJOR = 0x01;
        FIRST = 0x00;
        SECOND = 0x00;
        THRID = 0x00;
        send();
    }

    // 循迹
    public void Track(int sp_n) {  //寻迹
        MAJOR = 0x06;
        FIRST = (byte) (sp_n & 0xFF);
        SECOND = 0x00;
        THRID = 0x00;
        send();
    }

    //清除码盘值
    public void clear() {
        MAJOR = 0x07;
        FIRST = 0x00;
        SECOND = 0x00;
        THRID = 0x00;
        send();
    }

    //主从车状态转换
    public void vice(int i) {
        short temp = TYPE;
        if (i == 1) {//从车状态
            TYPE = 0x02;
            MAJOR = 0x80;
            FIRST = 0x01;
            SECOND = 0x00;
            THRID = 0x00;
            send();
            delay(500);

            TYPE = (byte) 0xAA;
            MAJOR = 0x80;
            FIRST = 0x01;
            SECOND = 0x00;
            THRID = 0x00;
            send();
            TYPE = 0x02;
        } else if (i == 2) {// 主车状态
            TYPE = 0x02;
            MAJOR = 0x80;
            FIRST = 0x00;
            SECOND = 0x00;
            THRID = 0x00;
            send();
            delay(500);

            TYPE = (byte) 0xAA;
            MAJOR = 0x80;
            FIRST = 0x00;
            SECOND = 0x00;
            THRID = 0x00;
            send();
            TYPE = 0xAA;
        }
        TYPE = temp;
    }

    // 红外
    public void infrared(byte one, byte two, byte thrid, byte four, byte five,
                         byte six) {
        MAJOR = 0x10;
        FIRST = one;
        SECOND = two;
        THRID = thrid;
        send();
        delay(500);
        MAJOR = 0x11;
        FIRST = four;
        SECOND = five;
        THRID = six;
        send();
        delay(500);
        MAJOR = 0x12;
        FIRST = 0x00;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        delay(1000);
    }

    // 双色led灯
    public void lamp(byte command) {
        MAJOR = 0x40;
        FIRST = command;
        SECOND = 0x00;
        THRID = 0x00;
        send();
    }

    // 指示灯
    public void light(int left, int right) {
        if (left == 1 && right == 1) {
            MAJOR = 0x20;
            FIRST = 0x01;
            SECOND = 0x01;
            THRID = 0x00;
            send();
        } else if (left == 1 && right == 0) {
            MAJOR = 0x20;
            FIRST = 0x01;
            SECOND = 0x00;
            THRID = 0x00;
            send();
        } else if (left == 0 && right == 1) {
            MAJOR = 0x20;
            FIRST = 0x00;
            SECOND = 0x01;
            THRID = 0x00;
            send();
        } else if (left == 0 && right == 0) {
            MAJOR = 0x20;
            FIRST = 0x00;
            SECOND = 0x00;
            THRID = 0x00;
            send();
        }
    }

    // 蜂鸣器
    public void buzzer(int i) {
        if (i == 1)
            FIRST = 0x01;
        else if (i == 0)
            FIRST = 0x00;
        MAJOR = 0x30;
        SECOND = 0x00;
        THRID = 0x00;
        send();
    }

    // 加光照档位
    public void gear(int i) {
        if (i == 1)
            MAJOR = 0x61;
        else if (i == 2)
            MAJOR = 0x62;
        else if (i == 3)
            MAJOR = 0x63;
        FIRST = 0x00;
        SECOND = 0x00;
        THRID = 0x00;
        send();
    }

    //立体显示
    public void infrared_stereo(short[] data) {
        MAJOR = 0x10;
        FIRST = 0xff;
        SECOND = data[0];
        THRID = data[1];
        send();
        delay(500);
        MAJOR = 0x11;
        FIRST = data[2];
        SECOND = data[3];
        THRID = data[4];
        send();
        delay(500);
        MAJOR = 0x12;
        FIRST = 0x00;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        delay(500);
    }

    //智能交通灯
    public void traffic_control(int type, int major, int first) {
        byte temp = (byte) TYPE;
        TYPE = (short) type;
        MAJOR = (byte) major;
        FIRST = (byte) first;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        TYPE = temp;
    }

    //立体车库控制
    public void garage_control(int type, int major, int first) {
        byte temp = (byte) TYPE;
        TYPE = (short) type;
        MAJOR = (byte) major;
        FIRST = (byte) first;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        TYPE = temp;
    }

    //openmv摄像头
    public void openmv_control(int major, int first) {
        byte temp = (byte) TYPE;
        TYPE = 0x02;
        MAJOR = (byte) major;
        FIRST = (byte) first;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        TYPE = temp;
    }

    // 闸门
    public void gate(int major, int first, int second, int third) {
        byte temp = (byte) TYPE;
        TYPE = 0x03;
        MAJOR = (byte) major;
        FIRST = (byte) first;
        SECOND = (byte) second;
        THRID = (byte) third;
        send();
        TYPE = temp;
    }

    //LCD 显示标志物进入计时模式
    public void digital_close() {//数码管关闭
        byte temp = (byte) TYPE;
        TYPE = 0x04;
        MAJOR = 0x03;
        FIRST = 0x00;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        TYPE = temp;
    }

    //数码管打开
    public void digital_open() {
        byte temp = (byte) TYPE;
        TYPE = 0x04;
        MAJOR = 0x03;
        FIRST = 0x01;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        TYPE = temp;
    }

    //数码管清零
    public void digital_clear() {
        byte temp = (byte) TYPE;
        TYPE = 0x04;
        MAJOR = 0x03;
        FIRST = 0x02;
        SECOND = 0x00;
        THRID = 0x00;
        send();
        TYPE = temp;
    }

    //LCD显示标志物第二排显示距离
    public void digital_dic(int dis) {
        byte temp = (byte) TYPE;
        int a = 0, b = 0, c = 0;
        a = (dis / 100) & (0xF);
        b = (dis % 100 / 10) & (0xF);
        c = (dis % 10) & (0xF);
        b = b << 4;
        b = b | c;
        TYPE = 0x04;
        MAJOR = 0x04;
        FIRST = 0x00;
        SECOND = (short) (a);
        THRID = (short) (b);
        send();
        TYPE = temp;
    }

    // 数码管
    public void digital(int i, int one, int two, int three) {
        byte temp = (byte) TYPE;
        TYPE = 0x04;
        if (i == 1) {//数据写入第一排数码管
            MAJOR = 0x01;
            FIRST = (byte) one;
            SECOND = (byte) two;
            THRID = (byte) three;
        } else if (i == 2) {//数据写入第二排数码管
            MAJOR = 0x02;
            FIRST = (byte) one;
            SECOND = (byte) two;
            THRID = (byte) three;
        }
        send();
        TYPE = temp;
    }

    //语音播报随机指令
    public void VoiceBroadcast() {
        byte temp = (byte) TYPE;
        TYPE = (short) 0x06;
        MAJOR = (short) 0x20;
        FIRST = (byte) 0x01;
        SECOND = (byte) 0x00;
        THRID = (byte) 0x00;
        send();
        TYPE = temp;
    }

    //tft lcd
    public void TFT_LCD(int type, int MAIN, int KIND, int COMMAD, int DEPUTY) {
        byte temp = (byte) TYPE;
        TYPE = (short) type;
        MAJOR = (short) MAIN;
        FIRST = (byte) KIND;
        SECOND = (byte) COMMAD;
        THRID = (byte) DEPUTY;
        send();
        TYPE = temp;
    }

    //磁悬浮
    public void magnetic_suspension(int MAIN, int KIND, int COMMAD, int DEPUTY) {
        byte temp = (byte) TYPE;
        TYPE = (short) 0x0A;
        MAJOR = (short) MAIN;
        FIRST = (byte) KIND;
        SECOND = (byte) COMMAD;
        THRID = (byte) DEPUTY;
        send();
        TYPE = temp;
    }

    // 延时函数
    public void delay(int time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    //语音识别
    public void yuyin(int mode, int num) {
        MAJOR = 0xb1;
        FIRST = (short) mode;
        SECOND = (short) num;
        THRID = 0;
        send();
    }

    private byte[] bytesend(byte[] sbyte) {
        byte[] textbyte = new byte[sbyte.length + 5];
        textbyte[0] = (byte) 0xFD;
        textbyte[1] = (byte) (((sbyte.length + 2) >> 8) & 0xff);
        textbyte[2] = (byte) ((sbyte.length + 2) & 0xff);
        textbyte[3] = 0x01;// 合成语音命令
        textbyte[4] = (byte) 0x01;// 编码格式
        for (int i = 0; i < sbyte.length; i++) {
            textbyte[i + 5] = sbyte[i];
        }
        return textbyte;
    }

    /**
     * 闸门控制
     * 参数一：
     * 1、开
     * 2、关
     * 3、显示车牌，参数二：车牌号
     * 4、请求返回道闸状态
     */
    public void gate_Control(int what, String plate) {
        switch (what) {
            case 0:
                // 打开闸门
                gate(0x01, 0x01, 0x00, 0x00);
                break;
            case 1:
                // 关闭闸门
                gate(0x01, 0x02, 0x00, 0x00);
                break;
            case 2:
                //显示车牌
                if (plate == null || plate.length() != 6) {
                    Log.e(TAG, "输入参数有误");
                    return;
                }
                char[] b = plate.toCharArray();
                gate(0x10, b[0], b[1], b[2]);
                delay(500);
                gate(0x11, b[3], b[4], b[5]);
                break;
            case 3:
                //请求返回道闸状态
                gate(0x20, 0x01, 0x00, 0x00);
                break;
            default:
                break;
        }
    }

    /**
     * 数码管显示控制
     * 参数一：
     * 1：计时开始
     * 2：计时结束
     * 3：计时清零
     * 4：显示任意数字，参数二：“显示层(1或2),XX,XX,XX”（逗号隔开）
     * 5:显示距离，参数二："距离"
     */
    public void digital_Control(int what, String digital) {
        switch (what) {
            case 1:
                //计时开始
                digital_open();
                break;
            case 2:
                //计时结束
                digital_close();
                break;
            case 3:
                //计时清零
                digital_clear();
                break;
            case 4:
                //显示任意数字
                if (digital == null || digital.length() != 4) {
                    Log.e(TAG, "输入参数有误");
                    return;
                }
                int main, one, two, three;
                String[] s = digital.split(",");
                main = Integer.parseInt(s[0]);
                one = Integer.parseInt(s[1], 16);
                two = Integer.parseInt(s[2], 16);
                three = Integer.parseInt(s[3], 16);
                digital(main, one, two, three);
                break;
            case 5:
                //显示距离
                if (digital == null || digital.equals("")) {
                    Log.e(TAG, "输入参数有误");
                    return;
                }
                digital_dic(Integer.parseInt(digital));
                break;
            default:
                break;
        }
    }

    /**
     * 语音播报控制
     * 参数一：
     * 1、播报随机指令
     * 2、播报指定内容，参数二：播报内容
     */
    public void voice_Control(int what, String voice) {
        switch (what) {
            case 1:
                VoiceBroadcast();
                break;
            case 2:
                if (voice == null) {
                    Log.e(TAG, "输入参数有误");
                    return;
                }
                try {
                    byte[] sbyte = bytesend(voice.getBytes("GBK"));
                    send_voice(sbyte);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
    }

    /**
     * 无线充电（磁悬浮）控制
     * 参数：
     * 1、打开
     * 2、关闭
     */
    public void magnetic_Control(int what) {
        switch (what) {
            case 1:
                //磁悬浮打开
                magnetic_suspension(0x01, 0x01, 0x00, 0x00);
                break;
            case 2:
                //磁悬浮关闭
                magnetic_suspension(0x01, 0x00, 0x00, 0x00);
                break;
            default:
                break;
        }
    }

    /**
     * TFT_LCD显示器控制
     * 参数一：
     * 1、显示器A
     * 2、显示器B
     * 参数二：
     * 图片显示模式：11、显示第1张，12、显示第2张，13、显示第3张，14.显示第4张，15、显示第5张，
     * 16、上翻一页，17、下翻一页，18、自动翻页
     * 计时模式：21：计时开始，22、计时关闭，23、计时停止
     * 3、车牌显示，参数三："车牌号"（6个字符）
     * 4、距离显示，参数三："距离"
     * 5、HEX显示模式，参数三："XX,XX,XX"
     */
    public void tft_Control(int tft, int what, String s) {
        byte type = 0x0B;
        if (tft == 1) {
            //显示器A
            type = 0x0B;
        } else if (tft == 2) {
            //显示器B
            type = 0x08;
        } else {
            Log.e(TAG, "参数错误");
        }
        switch (what) {
            case 11:
                //显示第一张图片
                TFT_LCD(type, 0x10, 0x01, 0x00, 0x00);
                break;
            case 12:
                //显示第二张图片
                TFT_LCD(type, 0x10, 0x02, 0x00, 0x00);
                break;
            case 13:
                //显示第三张图片
                TFT_LCD(type, 0x10, 0x03, 0x00, 0x00);
                break;
            case 14:
                //显示第四张图片
                TFT_LCD(type, 0x10, 0x04, 0x00, 0x00);
                break;
            case 15:
                //显示第五张图片
                TFT_LCD(type, 0x10, 0x05, 0x00, 0x00);
                break;
            case 16:
                //图片上翻一页
                TFT_LCD(type, 0x10, 0x01, 0x00, 0x00);//待修改
                break;
            case 17:
                //图片下翻一页
                TFT_LCD(type, 0x10, 0x02, 0x00, 0x00);
                break;
            case 18:
                //图片自动翻页
                TFT_LCD(type, 0x10, 0x03, 0x00, 0x00);
                break;
            case 21:
                //计时开始
                TFT_LCD(type, 0x30, 0x01, 0x00, 0x00);
                break;
            case 22:
                //计时结束
                TFT_LCD(type, 0x30, 0x02, 0x00, 0x00);
                break;
            case 23:
                //计时关闭
                TFT_LCD(type, 0x30, 0x00, 0x00, 0x00);
                break;
            case 3:
                //显示车牌
                if (s == null || s.length() != 6) {
                    Log.e(TAG, "输入参数有误");
                    return;
                }
                char[] b = s.toCharArray();
                TFT_LCD(type, 0x20, b[0], b[1], b[2]);
                delay(500);
                TFT_LCD(type, 0x21, b[3], b[4], b[5]);
                break;
            case 4:
                //显示距离
                if (s == null || s.equals("")) {
                    Log.e(TAG, "输入参数有误");
                    return;
                }
                int dis = Integer.parseInt(s);
                int a = 0, b1 = 0, c = 0;
                a = (dis / 100) & (0xF);
                b1 = (dis % 100 / 10) & (0xF);
                c = (dis % 10) & (0xF);
                b1 = b1 << 4;
                b1 = b1 | c;
                TFT_LCD(type, 0x50, 0x00, a, b1);
                break;
            case 5:
                //显示HEX
                int one, two, three;
                String[] ss = s.split(",");
                if (ss.length != 3) {
                    Log.e(TAG, "参数错误");
                    return;
                }
                one = Integer.parseInt(ss[0], 16);
                two = Integer.parseInt(ss[1], 16);
                three = Integer.parseInt(ss[2], 16);
                TFT_LCD(type, 0x40, one, two, three);
                break;
            default:
                break;
        }
    }

    /**
     * 智能交通灯控制
     * 参数一：
     * 1、交通灯A，
     * 2、交通灯B
     * 参数二：
     * 1、进入识别模式
     * 2、识别结果为红色，请求确认
     * 3、识别结果为绿色，请求确认
     * 4、识别结果为黄色，请求确认
     */
    public void traffic_Control(int what) {
        switch (what) {
            case 1:
                traffic_control(0x0E, 0x01, 0x00);
                break;
            case 2:
                traffic_control(0x0E, 0x02, 0x01);
                break;
            case 3:
                traffic_control(0x0E, 0x02, 0x02);
                break;
            case 4:
                traffic_control(0x0E, 0x02, 0x03);
                break;
            default:
                break;
        }
    }

    /**
     * 立体车库控制
     * 参数一：
     * 1、车库A
     * 2、车库B
     * 参数二：
     * 1、到达第一层
     * 2、到达第二层
     * 3、到达第三层
     * 4、到达第四层
     * 5、请求返回车库位于第几层
     * 6、请求返回前后侧红外状态
     */
    public void stereo_Control(int stero, int what) {
        int type = 0x0D;
        if (stero == 1) {
            type = 0x0D;
        } else if (stero == 2) {
            type = 0x05;
        }
        switch (what) {
            case 1:
                //到达第一层
                garage_control(type, 0x01, 0x01);
                break;
            case 2:
                //到达第二层
                garage_control(type, 0x01, 0x02);
                break;
            case 3:
                //到达第三层
                garage_control(type, 0x01, 0x03);
                break;
            case 4:
                //到达第四层
                garage_control(type, 0x01, 0x04);
                break;
            case 5:
                //请求返回车库位于第几层
                garage_control(type, 0x02, 0x01);
                break;
            case 6:
                //请求返回前后侧红外状态
                garage_control(type, 0x02, 0x02);
                break;
            default:
                break;
        }
    }

    /**
     * 报警器控制
     * 参数：
     * 1、开
     * 2、关
     */
    public void police_Control(int what) {
        switch (what) {
            case 1:
                //打开报警器
                infrared((byte) 0x03, (byte) 0x05, (byte) 0x14, (byte) 0x45, (byte) 0xDE, (byte) 0x92);
                break;
            case 2:
                //关闭报警器
                infrared((byte) 0x67, (byte) 0x34, (byte) 0x78, (byte) 0xA2, (byte) 0xFD, (byte) 0x27);
                break;
            default:
                break;
        }
    }

    /**
     * 光强挡位控制
     * 参数：
     * 1、光强加1档
     * 2、光强加2当
     * 3、光强加3当
     */
    public void gear_Control(int what) {
        switch (what) {
            case 1:
                //光强加1档
                gear(1);
                break;
            case 2:
                //光强加2档
                gear(2);
                break;
            case 3:
                //光强加3档
                gear(3);
                break;
            default:
                break;
        }
    }

    /**
     * 立体显示控制
     * 参数一：
     * 颜色信息：11、红色，12、绿色，13、蓝色，14、黄色，15、紫色
     * 图形信息：21、矩形，22、圆形，23、三角行，24、菱形，25、梯形，26、饼形，27、靶形，28、条形图
     * 路况信息：31、"隧道有事故，请绕行"，32、"前方施工，请绕行"
     * 4、距离信息，参数二：“距离”
     * 5、车牌信息，参数三：“车牌”（6个字符）
     */
    public void stero_display_Control(int what, String s) {
        short[] data = {0x00, 0x00, 0x00, 0x00, 0x00};
        switch (what) {
            case 11:
            case 12:
            case 13:
            case 14:
            case 15:
                //颜色信息
                data[0] = 0x13;
                data[1] = (short) (what - 10);
                infrared_stereo(data);
                break;
            case 21:
            case 22:
            case 23:
            case 24:
            case 25:
            case 26:
            case 27:
            case 28:
                //形状信息
                data[0] = 0x12;
                data[1] = (short) (what - 20);
                infrared_stereo(data);
                break;
            case 31:
            case 32:
                //路况信息
                data[0] = 0x14;
                data[1] = (short) (what - 30);
                infrared_stereo(data);
                break;
            case 4:
                //距离信息
                if (s == null || s.length() == 0) {
                    Log.e(TAG, "输入参数有误");
                    return;
                }
                int dis = Integer.parseInt(s);
                data[0] = 0x11;
                data[1] = (short) (dis / 10 + 0x30);
                data[2] = (short) (dis % 10 + 0x30);
                infrared_stereo(data);
                break;
            case 5:
                //车牌信息
                if (s == null || s.length() != 6) {
                    Log.e(TAG, "输入参数有误");
                    return;
                }
                char[] li = s.toCharArray();
                data[0] = 0x20;
                data[1] = (short) (li[0]);
                data[2] = (short) (li[1]);
                data[3] = (short) (li[2]);
                data[4] = (short) (li[3]);
                infrared_stereo(data);
                Log.i(TAG, "data___: " + Arrays.toString(data));
                data[0] = 0x10;
                data[1] = (short) (li[4]);
                data[2] = (short) (li[5]);
                data[3] = (short) (li[6]);
                data[4] = (short) (li[7]);
                infrared_stereo(data);
                break;
            case 6:
                data[0] = 0x15;
                data[1] = 0x01;
                infrared_stereo(data);
                break;
            default:
                break;
        }


    }

    /**
     * 摄像头位置为控制
     * 参数一：
     * 1~4、上下左右转动
     * 5~7、设置预设位1到3
     * 8~10、调用预设位1到3
     */
    public void camera_Control(int what, int step) {
        switch (what) {
            //上下左右转动
            case 1:
            case 2:
            case 3:
            case 4:
                LeftFragment.cameraCommandUtil.postHttp(FirstActivity.IPCamera, 2 * (what - 1), step);
                break;
            // / 5-7   设置预设位1到3
            case 5:
            case 6:
            case 7:
                LeftFragment.cameraCommandUtil.postHttp(FirstActivity.IPCamera, 2 * (what - 5) + 30, step);
                break;
            //调用预设位1-3
            case 8:
            case 9:
            case 10:
                LeftFragment.cameraCommandUtil.postHttp(FirstActivity.IPCamera, 2 * (what - 8) + 31, 0);
                break;
            default:
                break;
        }
    }

    /**
     * 蜂鸣器控制
     * 参数：
     * 1、开
     * 2、关
     */
    public void buzzer_Control(int what) {
        if (what == 1) {
            buzzer(1);
        } else {
            buzzer(0);
        }
    }

    /**
     * 二维码识别
     */
    public void qrcode_Reconize() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (LeftFragment.bitmap == null) {
                    timer.cancel();
                    myHandler.sendEmptyMessage(11);
                    return;
                }
                Result result = null;
                RGBLuminanceSource rSource = new RGBLuminanceSource(LeftFragment.bitmap);
                try {
                    BinaryBitmap binaryBitmap = new BinaryBitmap(
                            new HybridBinarizer(rSource));
                    Map<DecodeHintType, String> hint = new HashMap<DecodeHintType, String>();
                    hint.put(DecodeHintType.CHARACTER_SET, "utf-8");
                    QRCodeReader reader = new QRCodeReader();
                    result = reader.decode(binaryBitmap, hint);
                    if (result.toString() != null) {
                        result_qr = result.toString();
                        timer.cancel();
                        myHandler.sendEmptyMessage(10);
                    }
                    Log.i(TAG, "正在识别...");
                } catch (NotFoundException e) {
                    e.printStackTrace();
                } catch (ChecksumException e) {
                    e.printStackTrace();
                } catch (FormatException e) {
                    e.printStackTrace();
                }
            }
        }, 0, 200);
    }

    /**
     * 指示灯控制
     * 参数：
     * 1、左亮
     * 2、全亮
     * 3、右亮
     * 4、全灭
     */
    public void light_Control(int what) {
        switch (what) {
            case 1:
                //左亮
                light(1, 0);
                break;
            case 2:
                //全亮
                light(1, 1);
                break;
            case 3:
                //右亮
                light(0, 1);
                break;
            case 4:
                //全灭
                light(0, 0);
                break;
            default:
                break;
        }
    }

    /**
     * 车牌识别
     */
    public void plate_Reconize() {
        Bitmap bitmap = LeftFragment.bitmap;
        String name = date.getTime() + ".jpg";
        final File image = FileUtil.getOutputFile(name, FileUtil.IMAGE_PATH);
        try {
            FileOutputStream out = new FileOutputStream(image);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
            Log.i(TAG, "bitmap已经保存");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.i(TAG, "file path: " + image.getAbsolutePath());
        XcApplication.executorServicetor.execute(new Runnable() {
            @Override
            public void run() {
                result_plate = FirstActivity.plateRecognizer.recognize(image.getAbsolutePath());
                if (result_plate != null) {
                    Log.i(TAG, "识别成功");
                    myHandler.sendEmptyMessage(30);
                } else {
                    Log.i(TAG, "文件不存在");
                    myHandler.sendEmptyMessage(31);
                }
            }
        });
    }

    /**
     * 形状识别
     */
    public ArrayList shape_Reconize() {
        return shape.shape(LeftFragment.bitmap);
    }

    /**
     * Openmv控制
     * 参数：
     * 1、开启识别二维码
     * 2、关闭识别
     */
    public void openmv_Control(int what) {
        if (what == 1) {
            openmv_control(0x92, 0x01);
        } else {
            openmv_control(0x92, 0x02);
        }
    }

    //开始
    public void start1(int p) {
        MAJOR = 0xb2;
        FIRST = (short) p;
        SECOND = 0;
        THRID = 0;
        send();
    }

    //等待停止标志位
    public void wait_flag(int type) {
        int i = 0;
        while (Stop_Flag != type) {
            delay(10);
            i++;
            if (i == 1000) break;
        }
    }

    //到达
    public void Go_to(int position) {
        int x = position % 3;
        int y = position / 3;
        _Go_to(x, y);
    }

    public void _Go_to(int x1, int y1) {
        int X = POSITION % 3;
        int Y = POSITION / 3;
        int t = y1 > Y ? y1 - Y : Y - y1;
        if (y1 > Y) {
            if (DIR == 2) {
                Left(80);
                wait_flag(2);
                DIR = 1;
            } else if (DIR == 3) {
                Left(80);
                wait_flag(2);
                Left(80);
                wait_flag(2);
                DIR = 1;
            } else if (DIR == 4) {
                Right(80);
                wait_flag(2);
                DIR = 1;
            }
        } else if (y1 < Y) {
            if (DIR == 4) {
                Left(80);
                wait_flag(2);
                DIR = 3;
            } else if (DIR == 1) {
                Left(80);
                wait_flag(2);
                Left(80);
                wait_flag(2);
                DIR = 3;
            } else if (DIR == 2) {
                Right(80);
                wait_flag(2);
                DIR = 3;
            }
        }
        for (int i = 0; i < t; i++) {
            Track(80);
            wait_flag(1);
            go(80, 400);
            wait_flag(3);
        }
        Y = y1;
        t = x1 > X ? x1 - X : X - x1;
        if (x1 > X) {
            if (DIR == 3) {
                Left(80);
                wait_flag(2);
                DIR = 2;
            } else if (DIR == 4) {
                Left(80);
                wait_flag(2);
                Left(80);
                wait_flag(2);
                DIR = 2;
            } else if (DIR == 1) {
                Right(80);
                wait_flag(2);
                DIR = 2;
            }
        } else if (x1 < X) {
            if (DIR == 1) {
                Left(80);
                wait_flag(2);
                DIR = 4;
            } else if (DIR == 2) {
                Left(80);
                wait_flag(2);
                Left(80);
                wait_flag(2);
                DIR = 4;
            } else if (DIR == 3) {
                Right(80);
                wait_flag(2);
                DIR = 4;
            }
        }
        for (int i = 0; i < t; i++) {
            Track(80);
            wait_flag(1);
            go(80, 400);
            wait_flag(3);
        }
        X = x1;
    }

    //全自动
    public void aotu() {
        Log.d(TAG, "aotu running:");
        digital_open();
        delay(200);
        Go_to(3);
        Right(80);
        wait_flag(2);
        gate(0x01, 0x01, 0, 0);
        delay(1000);
        //while (Stop_Flag!=0x05);
        Go_to(4);
        Left(80);
        wait_flag(2);
        //while (Stop_Flag!=0x06);
        Go_to(7);
        Go_to(8);
        for (int i = 0; i < 4; i++) {
            gear(1);
            delay(1000);
        }
        Go_to(11);
        yuyin(0x01, 0);
        wait_flag(0x4f);
        Go_to(9);
        infrared((byte) 0x03, (byte) 0x05, (byte) 0x14, (byte) 0x45, (byte) 0xDE, (byte) 0x92);
        delay(1000);
        Go_to(3);
        Left(80);
        wait_flag(2);
        Left(80);
        wait_flag(2);
        Track(80);
        //while (Stop_Flag!=0x01)yanchi(50);
        delay(5000);
        stop();
        delay(200);
        back(80, 1500);
        wait_flag(3);
        magnetic_suspension(0x01, 0x01, 0x00, 0x00);
        delay(200);
        digital_close();
    }

}
